class OAuthModel {
  String? accessToken;
  String? tokenType;
  String? refreshToken;
  int? expiresIn;
  String? scope;
  String? jti;
  String? expiresTime;

  OAuthModel({
    this.accessToken,
    this.tokenType,
    this.refreshToken,
    required int this.expiresIn,
    this.scope,
    this.jti,
  }) : expiresTime = DateTime.now()
            // 提前20s过期(防止请求弱网络无法刷新token的问题)
            .add(Duration(seconds: expiresIn - 20))
            .toIso8601String();

  OAuthModel.fromJson(Map<String, dynamic> json) {
    accessToken = json['access_token'];
    tokenType = json['token_type'];
    refreshToken = json['refresh_token'];
    expiresIn = json['expires_in'];
    scope = json['scope'];
    jti = json['jti'];
    if (json['expiresTime'] == null) {
      expiresTime = DateTime.now()
          // 提前20s过期(防止请求弱网络无法刷新token的问题)
          .add(Duration(seconds: expiresIn! - 20))
          .toIso8601String();
    } else {
      expiresTime = json['expiresTime'];
    }
  }

  Map<String, dynamic> toJson() {
    final data = new Map<String, dynamic>();
    data['access_token'] = accessToken;
    data['token_type'] = tokenType;
    data['refresh_token'] = refreshToken;
    data['expires_in'] = expiresIn;
    data['scope'] = scope;
    data['jti'] = jti;
    data['expiresTime'] = expiresTime;
    return data;
  }

  bool isExpire() {
    return DateTime.parse(expiresTime!).isBefore(DateTime.now());
  }
}
